//
//  OldAccount.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/15.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "OldAccount.h"
#import "oldNetWork.h"
#import "QNuploader.h"

@implementation OldAccount

NSString * const OldAccountKey = @"OldAccountKey";

@synthesize accountInfo = _accountInfo;

+ ( instancetype ) shareInstance
{
    static OldAccount * instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once( &onceToken, ^
                  {
                      instance = [self new];
                  });
    return instance;
}

- ( id ) init
{
    if( self = [super init] )
    {
        self.accountInfo = [[oldServer alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:OldAccountKey]];
    }
    return self;
}

- (void)initAccountInfo:(block)block
{
    [OLDNetWork httpRequestWithPostPut:URL_MyInfo params:nil success:^(NSDictionary *dic) {
        
        self.accountInfo = [[oldServer alloc] initWithDictionary:dic];
        if(block)
        {
            block(YES);
        }
    } error:^(NSString *error) {
        if(block)
        {
            block(NO);
        }
    }];
}

- (void)initAccountByDic:(NSDictionary *)dic
{
    self.accountInfo = [[oldServer alloc] initWithDictionary:dic];
    [QiNiuShareInstance getQiNiuToken];
}

- (void)setAccountInfo:(oldServer *)accountInfo
{
    _accountInfo = accountInfo;
    NSDictionary *dic = [_accountInfo covertToDictionary];
    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:OldAccountKey];
}

- (oldServer *)accountInfo
{
    if (!_accountInfo) {
        NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:OldAccountKey];
        _accountInfo = [[oldServer alloc] initWithDictionary:dic];
    }
    
    return _accountInfo;
}

- (void)clearAccountInfo{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:OldAccountKey];
    [OLDNetWork clearToken];
    [OLDNetWork tokenError:@"登出成功"];
}

- (BOOL)ifCanServer
{
    if (([self.accountInfo.addtion.approveStatus integerValue] == Approve_passed) && ([self.accountInfo.addtion.serveStatus integerValue] == Server_OpenStatus)) {
        return YES;
    }
    
    return NO;
}


@end
